package com.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dao.daoCar;
import com.dao.daoGoods;
import com.dao.daoOrder;
import com.domain.AllCart;
import com.domain.Goods;
import com.domain.Order;
import com.domain.OrderInfo;
import com.domain.OrderTitle;

/**
 * 
 * 功能描述：订单业务方法
 * 
 * Version： 1.0
 * 
 * date： 2016年6月26日 上午1:39:08
 * 
 * @author：XiaoBingBy
 *
 */
public class orderService {

	/**
	 * 添加订单方法 添加订单时，要删除购物车数据
	 * @param allCatInfo 购物车信息
	 * @param username 用户名
	 * @param uid 8位订单号
	 */
	public void addOrder(String uid, String username, List<AllCart> allCatInfo) {
		/*private String order_flag;// 订单标志
		private String order_id;// 订单号
		private String username;// 用户名
		private String goods_id;// 商品id
		private String goods_name;// 商品名称
		private String goods_pic;// 商品图片
		private String goods_price;// 商品价格
		private String order_state;// 订单状态
		private String order_date;// 订单时间*/		
		daoOrder dao = new daoOrder();	//操作数据库
		dao.createOrder(uid, username, allCatInfo, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//把订单插入数据库
		//删除购物车数据
		daoCar daoCar = new daoCar();
		daoCar.deleteAllCar(username);
	}

	/**
	 * 添加一个商品的订单
	 * @param uuid
	 * @param username
	 * @param parameter
	 */
	public void addOnOrder(String uuid, String username, String goods) {
		daoGoods daoGoods = new daoGoods();
		Goods goodsInfo = daoGoods.selectGoodsInfo(goods);
		
		daoOrder daoOrder = new daoOrder();
		daoOrder.createOnOrder(uuid, username, goodsInfo);
	}

	/**
	 * 生成订单业务方法
	 * @param username
	 * @return
	 */
	public ArrayList<OrderInfo> getOrderService(String username) {
		daoOrder daoorder = new daoOrder();
		List<OrderTitle> selectOrderId = daoorder.selectOrderId(username);
		ArrayList<OrderInfo> list = new ArrayList<>();
		for (OrderTitle orderId : selectOrderId) {
			List<Order> selectGoodsId = daoorder.selectGoodsId(orderId.getOrder_id());
			OrderInfo orderInfo = new OrderInfo();
			orderInfo.setOrderHader(orderId);
			orderInfo.setOrDerBody(selectGoodsId);
			orderId.setAll_price(allPrice(selectGoodsId));
			allPrice(selectGoodsId, selectGoodsId.size());
			list.add(orderInfo);
		}
		return list;
	}
	
	/**
	 * 计算总价业务方法
	 * @param selectGoodsId
	 * @return
	 */
	public String allPrice(List<Order> selectGoodsId) {
		double i = 0;
		for (Order order : selectGoodsId) {
			Double x = Double.valueOf(order.getGoods_price());
			int y = Integer.valueOf(order.getCart_quantity());
			i+=x*y;
		}
		
		return String.valueOf(i);
	}
	
	/**
	 * 计算一条商品总价格  并设置到传入的数据类型中
	 * @param selectGoodsId
	 * @return
	 */
	public void allPrice(List<Order> selectGoodsId , int len) {
		for (int i = 0; i < len; i++) {
			double sum = 0;
			double x = Double.valueOf(selectGoodsId.get(i).getGoods_price());
			int y = Integer.valueOf(selectGoodsId.get(i).getCart_quantity());
			sum = x*y;
			selectGoodsId.get(i).setAll_price(String.valueOf(sum));
		}
	}

}
